import { computed, onMounted } from "vue";
import { useListPage } from "@/composables/useListPage";
import refundApi, {
  type RefundListParams,
  type RefundListResult,
  type RefundRecord
} from "@/api/system/order/refund";

interface RefundFilterForm {
  keyword: string;
  status: string | number;
  order_status: string | number;
  order_refund_status: string | number;
}

const DEFAULT_FILTER: RefundFilterForm = {
  keyword: "",
  status: "",
  order_status: "",
  order_refund_status: ""
};

const ORDER_STATUS_OPTIONS = [
  { label: "全部订单状态", value: "" },
  { label: "待确认", value: 0 },
  { label: "待支付", value: 1 },
  { label: "待客服指派", value: 2 },
  { label: "待物流安排", value: 3 },
  { label: "待卖家发货", value: 4 },
  { label: "运输中", value: 5 },
  { label: "已完成", value: 6 },
  { label: "已取消", value: 7 },
  { label: "售后处理中", value: 8 }
];

const ORDER_REFUND_STATUS_OPTIONS = [
  { label: "全部售后状态", value: "" },
  { label: "无售后", value: 0 },
  { label: "处理中", value: 1 },
  { label: "已完成", value: 2 }
];

const REFUND_STATUS_OPTIONS = [
  { label: "全部售后单", value: "" },
  { label: "申请中", value: 0 },
  { label: "已通过", value: 1 },
  { label: "已拒绝", value: 2 },
  { label: "已完成", value: 3 }
];

const ORDER_REFUND_STATUS_LABELS: Record<number, string> = {
  0: "无售后",
  1: "处理中",
  2: "已完成"
};

const refundStatusTag = (status: number) => {
  switch (status) {
    case 1:
      return "success";
    case 2:
      return "danger";
    case 3:
      return "info";
    default:
      return "warning";
  }
};

export function useRefundList() {
  const {
    formState: filterForm,
    pagination,
    dataList,
    loading,
    onSearch,
    resetForm,
    handlePageChange,
    handleSizeChange
  } = useListPage<RefundFilterForm, RefundRecord, RefundListResult>(
    {
      fetch: params => refundApi.fetchList(params as RefundListParams)
    },
    DEFAULT_FILTER,
    {},
    {
      paramsAdapter: params => {
        const query: RefundListParams = {
          page: params.page,
          page_size: params.page_size
        };

        const keyword = params.keyword?.trim();
        if (keyword) {
          query.keyword = keyword;
        }

        if (params.status !== "" && params.status !== undefined) {
          query.status = Number(params.status);
        }

        if (params.order_status !== "" && params.order_status !== undefined) {
          query.order_status = Number(params.order_status);
        }

        if (
          params.order_refund_status !== "" &&
          params.order_refund_status !== undefined
        ) {
          query.order_refund_status = Number(params.order_refund_status);
        }

        return query;
      },
      responseAdapter: (response: RefundListResult) => {
        const paginationMeta = response?.pagination ?? {
          total: 0,
          pageSize: 10,
          currentPage: 1
        };

        return {
          list: response?.list ?? [],
          total: Number(paginationMeta.total ?? 0),
          pageSize: Number(paginationMeta.pageSize ?? 10),
          currentPage: Number(
            paginationMeta.currentPage ?? paginationMeta.page ?? 1
          )
        };
      }
    }
  );

  const tableData = computed(() => dataList.value ?? []);

  const columns: TableColumnList = [
    { label: "售后单号", prop: "refund_no", width: 180 },
    {
      label: "关联订单",
      prop: "order",
      minWidth: 200,
      align: "left",
      slot: "order"
    },
    {
      label: "买卖双方",
      prop: "participants",
      minWidth: 220,
      align: "left",
      slot: "participants"
    },
    {
      label: "金额信息",
      prop: "amount",
      minWidth: 220,
      align: "left",
      slot: "amount"
    },
    {
      label: "状态",
      prop: "status",
      width: 160,
      align: "left",
      slot: "status"
    },
    {
      label: "说明",
      prop: "remark",
      minWidth: 220,
      align: "left",
      slot: "remark"
    }
  ];

  const handleSearch = () => {
    filterForm.page = 1;
    onSearch();
  };

  const handleResetFilters = () => {
    resetForm();
    onSearch();
  };

  const orderRefundStatusLabel = (status: number | null | undefined) => {
    if (status === null || status === undefined) return "未知";
    return ORDER_REFUND_STATUS_LABELS[status] ?? "未知";
  };

  onMounted(() => {
    onSearch();
  });

  return {
    filterForm,
    pagination,
    tableData,
    columns,
    loading,
    refundStatusOptions: REFUND_STATUS_OPTIONS,
    orderStatusOptions: ORDER_STATUS_OPTIONS,
    orderRefundStatusOptions: ORDER_REFUND_STATUS_OPTIONS,
    handleSearch,
    handleResetFilters,
    handlePageChange,
    handleSizeChange,
    refundStatusTag,
    orderRefundStatusLabel
  };
}
